<template>
  <div class="tabs">
    <el-tag
      v-for="(tag, index) in tabsList"
      :key="tag.name"
      :closable="tag.name !== 'home'"
      :effect="$route.name === tag.name ? 'dark' : 'plain'"
      @click="changeMenu(tag)"
      @close="handleClose(tag, index)"
      size="small"
      >{{ tag.label }}</el-tag
    >
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';

export default {
  name: 'CommonTag',
  data() {
    return {};
  },
  computed: {
    ...mapState('tap', ['tabsList']),
  },
  methods: {
    ...mapMutations('tap', ['closeTag']),
    changeMenu(item) {
      this.$router.push({ name: item.name });
    },
    handleClose(tag, index) {
      const length = this.tabsList.length - 1;
      this.closeTag(tag);
      if (tag.name !== this.$route.name) {
        return;
      }
      if (index === length) {
        this.$router.push({
          name: this.tabsList[index - 1].name,
        });
      } else {
        this.$router.push({
          name: this.tabsList[index].name,
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.tabs {
  padding: 20px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>
